package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.vo.GiftChangeReportVo;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GiftChangeReport;
import com.ruoyi.system.service.IGiftChangeReportService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 礼物兑换记录Controller
 * 
 * @author ruoyi
 * @date 2024-12-01
 */
@Controller
@RequestMapping("/system/report")
public class GiftChangeReportController extends BaseController
{
    private String prefix = "system/report";

    @Autowired
    private IGiftChangeReportService giftChangeReportService;

    @Autowired
    private ISysUserService userService;

    @RequiresPermissions("system:report:view")
    @GetMapping()
    public String report()
    {
        return prefix + "/report";
    }

    /**
     * 查询礼物兑换记录列表
     */
    @RequiresPermissions("system:report:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GiftChangeReport giftChangeReport)
    {
        startPage();
        List<GiftChangeReport> list = giftChangeReportService.selectGiftChangeReportList(giftChangeReport);
        List<GiftChangeReportVo> giftChangeReportVos = new ArrayList<>();
        list.forEach(bean -> {
            SysUser sysUser = userService.selectUserById(bean.getCreateUser());
            GiftChangeReportVo build = GiftChangeReportVo.builder()
                    .giftName(bean.getGiftName())
                    .giftScore(bean.getGiftScore())
                    .createUser(sysUser.getUserName())
                    .mobile(bean.getMobile())
                    .socreBalance(bean.getSocreBalance())
                    .build();

            giftChangeReportVos.add(build);
        });
        return getDataTable(giftChangeReportVos);
    }

    /**
     * 导出礼物兑换记录列表
     */
    @RequiresPermissions("system:report:export")
    @Log(title = "礼物兑换记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GiftChangeReport giftChangeReport)
    {
        List<GiftChangeReport> list = giftChangeReportService.selectGiftChangeReportList(giftChangeReport);
        ExcelUtil<GiftChangeReport> util = new ExcelUtil<GiftChangeReport>(GiftChangeReport.class);
        return util.exportExcel(list, "礼物兑换记录数据");
    }

    /**
     * 新增礼物兑换记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存礼物兑换记录
     */
    @RequiresPermissions("system:report:add")
    @Log(title = "礼物兑换记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GiftChangeReport giftChangeReport)
    {
        return toAjax(giftChangeReportService.insertGiftChangeReport(giftChangeReport));
    }

    /**
     * 修改礼物兑换记录
     */
    @RequiresPermissions("system:report:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GiftChangeReport giftChangeReport = giftChangeReportService.selectGiftChangeReportById(id);
        mmap.put("giftChangeReport", giftChangeReport);
        return prefix + "/edit";
    }

    /**
     * 修改保存礼物兑换记录
     */
    @RequiresPermissions("system:report:edit")
    @Log(title = "礼物兑换记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GiftChangeReport giftChangeReport)
    {
        return toAjax(giftChangeReportService.updateGiftChangeReport(giftChangeReport));
    }

    /**
     * 删除礼物兑换记录
     */
    @RequiresPermissions("system:report:remove")
    @Log(title = "礼物兑换记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(giftChangeReportService.deleteGiftChangeReportByIds(ids));
    }
}
